package cn.mygweb.web.controller.interceptor;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.mygweb.common.pojo.JsonResult;
import cn.mygweb.common.utils.JsonUtils;

/** 
* <p>Title: OneInterceptor.java</p>
* <p>Description: 测试拦截器one</p>  
* @author gxb 
* @date 2019年5月23日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
public class OneInterceptor implements HandlerInterceptor{

	/**
	 * <p>Title: preHandle</p>
	 * <p>Description: 在处理请求之前进行调用（Controller方法调用之前）</p>
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("被One拦截器拦截，但是放行.......");
		return true;// true表示放行，不拦截
		
//		if(true) {
//			returnErrorMsg(response, JsonResult.errorMsg("被OneInterceptor拦截了！！！"));
//		}
//		return false;
	}

	/**
	 * <p>Title: postHandle</p>
	 * <p>Description: 在处理请求之后，视图被渲染之前被调用（Controller方法调用之后）</p>
	 * @param request
	 * @param response
	 * @param handler
	 * @param modelAndView
	 * @throws Exception
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	/**
	 * <p>Title: afterCompletion</p>
	 * <p>Description: 在整个请求结束之后被调用，也就是在DispatcherServlet渲染了对应的视图
	 * 	之后执行（主要是用于进行资源清理工作）</p>
	 * @param request
	 * @param response
	 * @param handler
	 * @param ex
	 * @throws Exception
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

	/**
	 * <p>Title: returnErrorMsg</p>
	 * <p>Description: 如果是ajax请求被拦截，返回JsonResult类型的提示信息</p>
	 * @param response
	 * @param result
	 * @throws IOException
	 */
	public void returnErrorMsg(HttpServletResponse response, JsonResult result) throws IOException {
		OutputStream out = null;
		try {
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/json");
			out = response.getOutputStream();
			out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(out != null) {
				out.close();
			}
		}
		
	}
	
}
